package leetbook.tree;

import datastruct.tree.TreeNode;

public class Solution230 {

    //中序遍历，i代表第几个遍历，res代表第k个的结果
    int i = 0, res = 0;
    public int kthSmallest(TreeNode root, int k) {
        midOrder(root,k);
        return res;
    }

    public void midOrder(TreeNode root, int k){
        if(root==null||i>=k) return;//剪枝，此时结果已找到无需再继续遍历
        midOrder(root.left,k);
        //中序
        if(++i==k){
            res = root.val;
            return;
        }
        midOrder(root.right,k);
    }

}
